<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.29
     from ptg.tnf on 19 December 2010 -->

<TITLE>Pattern-based Text Generator - Influencing PTG Output</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000" BACKGROUND="gifs/bg.gif">
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0" VALIGN=BOTTOM>
<TR VALIGN=BOTTOM>
<TD WIDTH="160" VALIGN=BOTTOM>
<A HREF="http://eli-project.sourceforge.net/">
<IMG SRC="gifs/elilogo.gif" BORDER=0>
</A>&nbsp;
</TD>
<TD WIDTH="25" VALIGN=BOTTOM>
<img src="gifs/empty.gif" WIDTH=25 HEIGHT=25>
</TD>
<TD ALIGN=LEFT WIDTH="475" VALIGN=BOTTOM>
<A HREF="index.html"><IMG SRC="gifs/title.png" BORDER=0></A>
</TD>
<!-- |DELETE FOR SOURCEFORGE LOGO|
<TD>
<a href="http://sourceforge.net/projects/eli-project">
<img
  src="http://sflogo.sourceforge.net/sflogo.php?group_id=70447&amp;type=13"
  width="120" height="30"
  alt="Get Eli: Translator Construction Made Easy at SourceForge.net.
    Fast, secure and Free Open Source software downloads"/>
</a>
</TD>
|DELETE FOR SOURCEFORGE LOGO| -->
</TR>
</TABLE>

<HR size=1 noshade width=785 align=left>
<TABLE BORDER=0 CELLSPACING=2 CELLPADDING=0>
<TR>
<TD VALIGN=TOP WIDTH="160">
<h4>General Information</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="index.html">Eli: Translator Construction Made Easy</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="gindex_1.html#SEC1">Global Index</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="faq_toc.html" >Frequently Asked Questions</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="ee.html" >Typical Eli Usage Errors</a> </td></tr>
</table>

<h4>Tutorials</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="EliRefCard_toc.html">Quick Reference Card</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="novice_toc.html">Guide For new Eli Users</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="news_toc.html">Release Notes of Eli</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="nametutorial_toc.html">Tutorial on Name Analysis</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="typetutorial_toc.html">Tutorial on Type Analysis</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="ee.html" >Typical Eli Usage Errors</a> </td></tr>
</table>

<h4>Reference Manuals</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="ui_toc.html">User Interface</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="pp_toc.html">Eli products and parameters</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="lidoref_toc.html">LIDO Reference Manual</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="ee.html" >Typical Eli Usage Errors</a> </td></tr>
</table>

<h4>Libraries</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="lib_toc.html">Eli library routines</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="modlib_toc.html">Specification Module Library</a></td></tr>
</table>

<h4>Translation Tasks</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="lex_toc.html">Lexical analysis specification</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="syntax_toc.html">Syntactic Analysis Manual</a></td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="comptrees_toc.html">Computation in Trees</a></td></tr>
</table>

<h4>Tools</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="lcl_toc.html">LIGA Control Language</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="show_toc.html">Debugging Information for LIDO</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="gorto_toc.html">Graphical ORder TOol</a> </td></tr>
</table>
<p>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="fw_toc.html">FunnelWeb User's Manual</a> </td></tr>
</table>
<p>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="ptg_toc.html">Pattern-based Text Generator</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="deftbl_toc.html">Property Definition Language</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="oil_toc.html">Operator Identification Language</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="tp_toc.html">Tree Grammar Specification Language</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="clp_toc.html">Command Line Processing</a> </td></tr>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="cola_toc.html">COLA Options Reference Manual</a> </td></tr>
</table>
<p>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="idem_toc.html">Generating Unparsing Code</a> </td></tr>
</table>
<p>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="mon_toc.html">Monitoring a Processor's Execution</a> </td></tr>
</table>

<h4>Administration</h4>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr valign=top><td><img src="gifs/gelbekugel.gif" WIDTH=7 HEIGHT=7 ALT=" o"> </td><td><a href="sysadmin_toc.html">System Administration Guide</a> </td></tr>
</table>

<HR WIDTH="100%">
<A HREF="mailto:eli-project-users@lists.sourceforge.net">
<IMG SRC="gifs/button_mail.gif" BORDER=0 ALIGN="left"></A>
<A HREF="index.html"><IMG SRC="gifs/home.gif" BORDER=0 ALIGN="right"></A>

</TD>
<TD VALIGN=TOP WIDTH="25"><img src="gifs/empty.gif" WIDTH=25 HEIGHT=25></TD>

<TD VALIGN=TOP WIDTH="600">
<H1>Pattern-based Text Generator</H1>
<P>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="ptg_6.html"><IMG SRC="gifs/prev.gif" ALT="Previous Chapter" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="ptg_8.html"><IMG SRC="gifs/next.gif" ALT="Next Chapter" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="ptg_toc.html"><IMG SRC="gifs/up.gif" ALT="Table of Contents" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT="">
<HR size=1 noshade width=600 align=left>
<H1><A NAME="SEC19" HREF="ptg_toc.html#SEC19">Influencing PTG Output</A></H1>
<P>
The usage of PTG Patterns functions is a very flexible way to construct
the output of a program. However, some 
desirable effects can not be achieved using
pattern functions only: 
<DL COMPACT>
<DT><STRONG>Pretty Printing</STRONG>
<DD>A pattern function does not have access to the current
column position. Therefore, it cannot know where to insert line breaks to
get the output formatted properly.
<DT><STRONG>Output destination</STRONG>
<DD>It can be desirable to write the output of PTG patterns to a destination 
other than a file,
for example to redirect the output into a
string buffer or to postprocess the output through a filter.
</DL>
<P>
To solve such tasks, PTG does not write its output directly into a
file. Instead, a set of macros is defined that can be adjusted to change
the behavior of PTG. If these macros are not defined, PTG supports default
definitions that process the contents of a <CODE>PTGNode</CODE> into a named file or a 
given file pointer. In the following, those macros are
explained and small examples are given:
<P>
<DL COMPACT>
<DT><CODE>PTG_OUTPUT_FILE</CODE>
<DD>This macro defines the type name of the <CODE>file</CODE> parameter of the
other macros, the output functions defined in  <A HREF="ptg_3.html#SEC7">Output Functions</A>, and the function
call insertions. If no definition is supplied, its value is defined to be
<CODE>FILE *</CODE>. Its value must be assignable by the C-operator 
<CODE>=</CODE>. If the definition of <CODE>PTG_OUTPUT_FILE</CODE> is changed, 
a suitable definition for <CODE>PTG_OUTPUT_STRING</CODE>
must also be provided.
<P>
<DT><CODE>PTG_OUTPUT_STRING(file,param)</CODE>
<DD>This macro is called to write a string value into an output file. 
It is used by default for every text written by PTG. 
Hence, redefining this macro suffices to change the default
behavior of PTG, for instance to support pretty printing.
This macro has to be redefined if 
<CODE>PTG_OUTPUT_FILE</CODE> is redefined.
<P>
<DT><CODE>PTG_OUTPUT_INT(file,param)</CODE>
<DD><DT><CODE>PTG_OUTPUT_SHORT(file,param)</CODE>
<DD><DT><CODE>PTG_OUTPUT_LONG(file,param)</CODE>
<DD><DT><CODE>PTG_OUTPUT_CHAR(file,param)</CODE>
<DD><DT><CODE>PTG_OUTPUT_FLOAT(file,param)</CODE>
<DD><DT><CODE>PTG_OUTPUT_DOUBLE(file,param)</CODE>
<DD>These macros are used to write typed insertions into 
an output file. By default, they are set up in a way that they convert
their second argument into a string and call <CODE>PTG_OUTPUT_STRING</CODE>
to process the output. So, when redefining <CODE>PTG_OUTPUT_FILE</CODE>, 
you need not to supply a definition for these macros.
<P>
Of course, there may be more efficient ways to output values of the various 
data types other than to convert them into strings and send the result
to the string handling function. If you want to supply such an alternative 
for writing characters, for example, redefine the default behavior of
<CODE>PTG_OUTPUT_CHAR</CODE> to print the character directly. These macros are
provided for efficiency purposes only.
</DL>
<P>
To override the default implementations for these macros, implement a
substitution function in a type <CODE>.c</CODE> file. Include the file
<CODE>ptg_gen.h</CODE> to supply your new function with definitions for the other
macros, especially <CODE>PTG_OUTPUT_FILE</CODE>. Write cpp directives
that define the desired macro(s) into a type <CODE>.ptg.phi</CODE> file and include
it in your specification. Eli will then concatenate all those definitions
and supply it as a header file <CODE>ptg.h</CODE> to your processor.
<P>
As an application of these macros, the following section shows a
simple and easy way to implement pretty printing of the output generated
by PTG.
<P>
<H2><A NAME="SEC20" HREF="ptg_toc.html#SEC20">Changing Default Output for Limited Line Length</A></H2>
<P>
Sometimes it is necessary to postprocess PTG generated output. For example,
the length of the generated output lines may be limited by an upper bound, 
so that certain restricted tools can process the output. 
Without postprocessing of the output, there is no way to determine the 
current position in a line for a PTG structure being written.
Hence, we adapt the definitions of the output macros to solve that task.
<P>
As all output is finally handled by the macro
<CODE>PTG_OUTPUT_STRING</CODE>, it is sufficient to modify it such that
it controls the current position in the line. With some more effort, it
would be possible do complete line breaking by buffering one line of output 
and looking for suitable break points when the end of the line is encountered.
<P>
The following code keeps track of the current column position and implements 
a function, that conditionally inserts a line break if the line is longer
than 65 chars.
<P>
<B>linepos.c</B>[22]==
<PRE>
#include &#60;string.h&#62;
#include "ptg_gen.h"
        
static col = 0;

void InitCol(void)
{
   col = 0;
}

void OutputLine(FILE *f, char *s)
{
   int l;
   char *nl;

   if (!s) return;

   l = strlen(s);
   nl = strrchr(s, '\n');

   if (!nl)
      col += l;
   else
      col = (l - 1 - (nl - s)); 
   fputs(s, f);
}

void CondNl(FILE *f)
{
   if (col &#62; 65)
      OutputLine(f, "\n");
}
</PRE>
<PRE>
This macro is attached to a product file.
</PRE>
<P>
The function <CODE>InitCol</CODE> serves as initialization, if more than one
output file should be generated.  The function <CODE>CondNl</CODE> inserts a
line break, if the current line is longer than 65 characters.  The
function <CODE>OutputLine</CODE> overrides the default implementation of
<CODE>PTG_OUTPUT_STRING</CODE>.
<P>
<B>linepos.ptg.phi</B>[23]==
<PRE>
#define PTG_OUTPUT_STRING(file,param) OutputLine(file,param)
</PRE>
<PRE>
This macro is attached to a product file.
</PRE>
<P>
This can be used for example in the following PTG specification:
<P>
<B>linepos.ptg</B>[24]==
<PRE>
CommaSeq:       $ ", " [CondNl] $
</PRE>
<PRE>
This macro is attached to a product file.
</PRE>
<P>
Now, in a large iteration of calls to <CODE>PTGCommaSeq()</CODE>, a line break
is inserted automatically, if a line exceeds 65 characters.
<P>
Using the online documentation, one can obtain a copy of the attached files.
<P>
<HR size=1 noshade width=600 align=left>
<P>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="ptg_6.html"><IMG SRC="gifs/prev.gif" ALT="Previous Chapter" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="ptg_8.html"><IMG SRC="gifs/next.gif" ALT="Next Chapter" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT=""><A HREF="ptg_toc.html"><IMG SRC="gifs/up.gif" ALT="Table of Contents" BORDER="0"></A>
<IMG SRC="gifs/empty.gif" WIDTH=25 HEIGHT=25 ALT="">
<HR size=1 noshade width=600 align=left>
</TD>
</TR>
</TABLE>

</BODY></HTML>
